import {IComponent, Application} from "pinus";
import {createPool, Pool, PoolConfig} from "mysql";
import {getLogger} from "pinus-logger";
const logger = getLogger("PinusMysql")

export default function(app: Application, opts: PoolConfig) {
    return new MysqlPool(app, opts)
}


export class MysqlPool implements IComponent{
    public name:string = "MysqlPool"
    private app: Application
    private options: PoolConfig
    private pool: Pool

    constructor(app: Application, opts: PoolConfig) {
        this.app = app
        this.options = opts
        this.pool = createPool(this.options)
        this.app.set(this.name, this.pool, true)
    }
    public start(cb: () => void) {
        cb()
    }

    public afterStart(cb: () => void) {
        logger.info('mysql afterStart');
        process.nextTick(cb);
    }

    public stop(force: boolean, cb: () => void) {
        this.pool.end((err) => {
            if (!!err) {
                console.log(err)
            }
            console.log('pool stop')
            cb()
        })
    }
}



